package cn.itcast.dewu.controller;


import cn.itcast.dewu.dto.Result;
import cn.itcast.dewu.pojo.Voucher;
import cn.itcast.dewu.service.VoucherService;
import cn.itcast.dewu.utils.RedisIdWork;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/*
* 优惠券
* */
@RestController
@RequestMapping("/voucher")
public class VoucherController {

    @Autowired
    private VoucherService voucherService;
    @Autowired
    private RedisIdWork idWork;
    /**
     * 新增普通券
     */
    @PostMapping
    public Result addVoucher(@RequestBody Voucher voucher) {
        voucher.setId(idWork.nextID("voucher"));
        voucherService.save(voucher);
        return Result.ok(voucher.getId());
    }

    /**
     * 新增秒杀券
     */
    @PostMapping("seckill")
    public Result addSeckillVoucher(@RequestBody Voucher voucher) {
        voucherService.addSeckillVoucher(voucher);
        return Result.ok(voucher.getId());
    }

    /**
     * 查询商品的优惠券列表
     */
    @GetMapping("/list/{goodsId}")
    public Result queryVoucherOfGoods(@PathVariable("goodsId") Long goodsId) {
       return voucherService.queryVoucherOfGoods(goodsId);
    }
}
